﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_CGSQD : Form
    {
        int cgdh;

        public Form_CGSQD(int cgd)
        {
            InitializeComponent();
            cgdh = cgd;
        }

        public Form_CGSQD()
        {
            InitializeComponent();
            try
            {
             using(SqlConnection con =new SqlConnection(PublicUnit.ConnetionString))
             {
                 SqlCommand com = new SqlCommand("SELECT MAX(CGDH)+1 FROM ML_CGD0",con);
                 con.Open();
                 SqlDataReader dr = com.ExecuteReader();
                 cgdh =Convert.ToInt32( dr[0].ToString().Trim());
             }
            }catch(SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            tbDH.Text = cgdh.ToString();
            lblDH.Text = "新" + lblDH.Text;
        }

        SqlConnection con;
        SqlDataAdapter sda;
        DataSet ds;
        BindingSource bs;
        SqlCommandBuilder builder;

        private void Form_CGSQD_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_GYS”中。您可以根据需要移动或移除它。
            this.dic_GYSTableAdapter.Fill(this.yKGLXTDataSet.Dic_GYS);
            #region 供应商初始化
            string sqlstr = "SELECT BM,MC FROM Dic_GYS";
            con = new SqlConnection(PublicUnit.ConnetionString);
            DataTable dtGYS = new DataTable();
            sda = new SqlDataAdapter(sqlstr, con);
            sda.Fill(dtGYS);
            cbGYS.DataSource = dtGYS;
            cbGYS.DisplayMember = "MC";
            cbGYS.ValueMember = "BM";
            #endregion

            con = new SqlConnection(PublicUnit.ConnetionString);
            string sCGSQD = "SELECT YPID,(SELECT YP.MC FROM Dic_YP YP,ML_CGJH1 CGJH1 WHERE CGJH1.YPID = YP.BM AND CGJH1.CGDH =" + cgdh +
                ") AS MC,GYSBM,BZSL,DJ,JE,SCSBM,KCSL,YXHSL,YJSL,SQCGSL,NGSL FROM ML_CGJH1 " +
                "WHERE CGDH = " + cgdh;
            sda = new SqlDataAdapter(sCGSQD, con);
            builder = new SqlCommandBuilder(sda);
            ds = new DataSet();
            bs = new BindingSource();
            dgvCGSQD.AutoGenerateColumns = true;
            try
            {
                sda.Fill(ds);
                dgvCGSQD.DataSource = ds.Tables[0];
                dgvCGSQD.Columns[0].HeaderText = "编码";
                dgvCGSQD.Columns[1].HeaderText = "名称";
                dgvCGSQD.Columns[2].HeaderText = "供应商";
                dgvCGSQD.Columns[3].HeaderText = "包装规格";
                dgvCGSQD.Columns[4].HeaderText = "单价";
                dgvCGSQD.Columns[5].HeaderText = "进价";
                dgvCGSQD.Columns[6].HeaderText = "生产商";
                dgvCGSQD.Columns[7].HeaderText = "库存";
                dgvCGSQD.Columns[8].HeaderText = "消耗";
                dgvCGSQD.Columns[9].HeaderText = "计划数";
                dgvCGSQD.Columns[10].HeaderText = "采购数";
                dgvCGSQD.Columns[11].HeaderText = "拟购数";
                bs.DataSource = ds.Tables[0];
                bNavigator.BindingSource = bs;
                dgvCGSQD.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgvCGSQD_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //检测是被表示的控件还是DataGridViewTextBoxEditingControl
            if (e.Control is DataGridViewTextBoxEditingControl)
            {
                DataGridView dgv = (DataGridView)sender;

                //取得被表示的控件
                DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;

                //事件处理器删除
                tb.KeyPress -= new KeyPressEventHandler(dgvCGSQD_KeyPress);

                //检测对应列
                if (dgv.CurrentCell.OwningColumn.Name == "单价")
                {
                    // KeyPress事件处理器追加
                    tb.KeyPress += new KeyPressEventHandler(dgvCGSQD_KeyPress);
                }
            }
        }

        private void dgvCGSQD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (dgvCGSQD.CurrentCell.ColumnIndex + 1 < dgvCGSQD.Columns.Count)
                {
                        dgvCGSQD.Focus();
                        if (dgvCGSQD.CurrentRow.Index + 1 < dgvCGSQD.Rows.Count)
                            dgvCGSQD.CurrentCell = dgvCGSQD[0, dgvCGSQD.CurrentCell.ColumnIndex + 1];
                        if (dgvCGSQD.CurrentRow.Index + 1 == dgvCGSQD.Rows.Count)
                            dgvCGSQD.CurrentCell = dgvCGSQD[0, dgvCGSQD.CurrentCell.ColumnIndex];
                }
            }
        }

        private void dgvCGSQD_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {

        }
    }
}
